package com.east.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.east.dto.ActivityRemarkDTO;
import com.east.dto.ClueRemarkDTO;
import com.east.entity.ClueRemark;
import com.east.entity.User;
import com.east.mapper.ClueRemarkMapper;
import com.east.query.ClueRemarkQuery;
import com.east.result.Result;
import com.east.service.ClueRemarkService;
import com.east.utils.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClueRemarkServiceImpl implements ClueRemarkService {
    @Resource
    private ClueRemarkMapper clueRemarkMapper;

    @Override
    public Result save(ClueRemarkDTO clueRemarkDTO) {
        ClueRemark clueRemark = BeanUtil.copyProperties(clueRemarkDTO, ClueRemark.class);
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        clueRemark.setCreateBy(loginUser.getId());
        clueRemark.setCreateTime(new Date());
        int insert = clueRemarkMapper.insert(clueRemark);
        return insert > 0 ? Result.OK() : Result.FAIL("添加失败");
    }

    @Override
    public Result list(Integer currentPage, Integer pageSize, Integer clueId) {
        Page<ClueRemarkDTO> page = PageHelper.startPage(currentPage, pageSize);
        List<ClueRemarkDTO> clueRemarkDTOS = clueRemarkMapper.list(clueId);
        PageData<ClueRemarkDTO> pageData = new PageData<>(currentPage, pageSize,
                Integer.valueOf(page.getTotal() + ""), page.getPages(), page.getResult());
        return Result.OK(pageData);
    }

    @Override
    public Result info(Integer clueRemarkId) {
        ClueRemarkDTO clueRemarkDTO = clueRemarkMapper.info(clueRemarkId);
        return Result.OK(clueRemarkDTO);
    }

    @Override
    public Result update(ClueRemarkDTO clueRemarkDTO) {
        ClueRemark clueRemark = BeanUtil.copyProperties(clueRemarkDTO, ClueRemark.class);
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        clueRemark.setEditBy(loginUser.getId());
        clueRemark.setEditTime(new Date());
        int i = clueRemarkMapper.updateByPrimaryKeySelective(clueRemark);
        return i > 0 ? Result.OK() : Result.FAIL("更新失败");
    }

    @Override
    public Result delete(Integer id) {
        int i = clueRemarkMapper.deleteLogical(id);
        return i > 0 ? Result.OK() : Result.FAIL("删除失败");
    }
}
